# 设置cmake最低版本
cmake_minimum_required(VERSION 3.2)

project(nng_bridge)

#set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CXX_STANDARD_REQUIRED ON)

message("CMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}")

#获取当前的编译架构
if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
  set(X86 1)
  set(PLATFORM "amd64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i686.*|i386.*|x86.*")
  set(X86 1)
  set(PLATFORM "i386")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*|arm64.*|ARM64.*)")
  set(ARM 1)
  set(PLATFORM "aarch64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm.*|ARM.*)")
  set(ARM 1)
  set(PLATFORM "arm")
endif()

message("PLATFORM:" ${PLATFORM})
if(${R328})
  set(ARM_R328 1)
  message("ARM_R328: " ${ARM_R328})
elseif(${ARM})
  set(ARM_RK3308 1)
  message("ARM_RK3308: " ${ARM_RK3308})
endif()

if(${MODE_TYPE})
else()
  set(MODE_TYPE 20)
endif()

message("MODEL_ACTIVED: ${MODE_TYPE}")
add_definitions(-DMODEL_ACTIVED=${MODE_TYPE})

# 生产clangd需要的中间文件
set(CMAKE_EXPORT_COMPILE_COMMANDS True)

#函数no_return的warning设置为error 
add_definitions(-Werror=return-type)
#add_definitions(-Wno-unused-result)
add_definitions(-Wno-psabi)





################################
# CMAKE_SKIP_BUILD_RPATH
#     当置为 false 时， 编译目录下的 ELF 文件将自带编译时的所有 rpath。
#     当置为 true 时， 编译目录下的 ELF 文件将不携带编译时的所有 rpath，状态和安装目录下的一样。
#     不影响安装的 ELF 文件。 安装的 ELF 文件不携带编译时的 rpath。
# [参考]
#     1. 探讨CMake中关于RPATH的使用 - Ricky.K - 博客园 https://www.cnblogs.com/rickyk/p/3875084.html
#     2. RPATH handling - CMake Wiki https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling
set(CMAKE_SKIP_BUILD_RPATH TRUE)

set(CMAKE_SKIP_RPATH FALSE)  # true 时令所有 rpath 相关的选项失效（被忽略），导致任何编译与安装的 ELF 文件里都没有 rpath。

#set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
#set(CMAKE_INSTALL_RPATH /opt/jomoo/3rdparty/lib)
set(CMAKE_INSTALL_RPATH /usr/local/lib)
set(CMAKE_BUILD_RPATH_USE_ORIGIN FALSE)

set(CMAKE_BUILD_RPATH '${CMAKE_INSTALL_RPATH}')  # 这个会在原有 rpath 基础上追加。 当 CMAKE_SKIP_BUILD_RPATH 为 true 时无效。

set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)  # true 会让安装的 rpath 添加上编译链接阶段用到的 path，包括当前工作空间的 devel/lib。





# 依赖
if(${ARM_RK3308})
  find_package(PkgConfig REQUIRED)
  pkg_search_module(NN REQUIRED nanomsg)
endif()

# 获得工作目录
set(WORKSPACE_PATH
    ".."
    CACHE PATH "")
execute_process(
  COMMAND readlink -f ${WORKSPACE_PATH}
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  OUTPUT_VARIABLE WORKSPACE_ABSOLUTE_PATH
  OUTPUT_STRIP_TRAILING_WHITESPACE)

message("Workspace Path: " ${WORKSPACE_ABSOLUTE_PATH})
if(${ARM_RK3308})
  set(MLX_UTILS_DIR "mlx_utils")
elseif(${ARM_R328})
  set(MLX_UTILS_DIR "mlx_utils")
else()
  set(MLX_UTILS_DIR "mlx_utils")
endif()

if(${ARM})
  set(MLX_UTILS_BUILD_DIR ${MLX_UTILS_DIR})
else()
  set(MLX_UTILS_BUILD_DIR ${MLX_UTILS_DIR}/build)
endif()


# 头文件依赖：mlx_utils
include_directories(${WORKSPACE_ABSOLUTE_PATH}/${MLX_UTILS_DIR})
include_directories(${WORKSPACE_ABSOLUTE_PATH}/${MLX_UTILS_DIR}/spdlog/include)
include_directories(${WORKSPACE_ABSOLUTE_PATH}/${MLX_UTILS_DIR}/rapidjson/include)
include_directories(${WORKSPACE_ABSOLUTE_PATH}/${MLX_UTILS_DIR}/mbedtls/include)

# 库依赖：mlx_utils
link_directories(${WORKSPACE_ABSOLUTE_PATH}/${MLX_UTILS_BUILD_DIR}/lib)
link_directories(${WORKSPACE_ABSOLUTE_PATH}/${MLX_UTILS_BUILD_DIR}/nanomsg)
link_directories(${WORKSPACE_ABSOLUTE_PATH}/${MLX_UTILS_BUILD_DIR}/spdlog)
link_directories(${WORKSPACE_ABSOLUTE_PATH}/${MLX_UTILS_BUILD_DIR}/lz4)
link_directories(${WORKSPACE_ABSOLUTE_PATH}/${MLX_UTILS_BUILD_DIR}/mbedtls)
link_directories(${WORKSPACE_ABSOLUTE_PATH}/${MLX_UTILS_BUILD_DIR}/mcv)

# 设置头文件目录
include_directories(./src)

# 获取路径下所有的.cpp/.c/.cc文件（不包括子目录），并赋值给变量中
aux_source_directory(./src SRC)
# aux_source_directory(./src/jomoo_mod SRC)

# 生成可执行文件
add_executable(${PROJECT_NAME}_pub_client
  src/pub_client.cpp
)

# # target_link_libraries(${PROJECT_NAME} pthread mlx_utils spdlog lz4  nanomsg  anl mbedtls)
target_link_libraries(${PROJECT_NAME}_pub_client
  pthread 
  # jomoo_utils
  nng
)


add_executable(${PROJECT_NAME}_pub_server
  src/pub_server.cpp
)

# # target_link_libraries(${PROJECT_NAME} pthread mlx_utils spdlog lz4  nanomsg  anl mbedtls)
target_link_libraries(${PROJECT_NAME}_pub_server
  pthread 
  jomoo_utils
  nng
)


add_executable(pubsub_forwarder
  src/pubsub_forwarder.cpp
)

target_link_libraries(pubsub_forwarder
  pthread 
  # jomoo_utils
  nng
)


# 指定lib文件生成目录
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

# 指定bin文件生成目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

install(TARGETS 
  ${PROJECT_NAME}_pub_client
  ${PROJECT_NAME}_pub_server
  pubsub_forwarder
  RUNTIME DESTINATION jomoo/bin
  LIBRARY DESTINATION jomoo/lib
  ARCHIVE DESTINATION jomoo/lib
)
